package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.viewModelScope
import com.ht.medicaltreatment.bean.CheckGroupBeanData
import com.ht.medicaltreatment.bean.CheckUidBean
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.launch

class CheckGroupViewModel : BaseViewModel() {
    fun requestData(id:String, onSuccess:(CheckGroupBeanData)->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.groupInfo(id)
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    //紧急跳过
    fun jump(orderId:String,sghyMarshallId:String, onSuccess:()->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.checkGroupJump(orderId,sghyMarshallId)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    //备注
    fun checkGroupRemarks(globalRemark:String,sghyMarshallId:String, onSuccess:()->Unit,
                          onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.checkGroupRemarks(globalRemark,sghyMarshallId)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    //溶血
    fun rongXue(isHemolysis:String,sghyMarshallId:String, onSuccess:()->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.rongXue(isHemolysis,sghyMarshallId)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    //脂血
    fun ziXue(isEsterBlood:String,sghyMarshallId:String, onSuccess:()->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.ziXue(isEsterBlood,sghyMarshallId)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    //分离时间
    fun updateTime(sghyMarshallId:Int, onSuccess:()->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.updateTime(sghyMarshallId)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    /**
     * 查询uid对应的名字
     */
    fun searchName(uid:MutableList<String>, onResult:(MutableList<String>)->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            when(val bean = mApi.searchNameScan(uid)){
                is ApiResult.Success -> {
                    val sampleInfoList = bean.data.data?.sampleInfoList
                    if(sampleInfoList != null && sampleInfoList.size > 0){
                        val list = arrayListOf<String>()
                        sampleInfoList.forEach {
                            list += it.name
                        }
                        onResult(list)
                    }else{
                        onError("无数据")
                    }
                }
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    //提交核验数据
    fun submitData(json: CheckUidBean, onSuccess:()->Unit, onError: (String) -> Unit){
        viewModelScope.launch {
            when(val bean = mApi.submitData(json)){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    //获取核验和分装时间
    fun getTime(id:String, onSuccess:(String?,String?)->Unit, onError: (String) -> Unit){
        viewModelScope.launch {
            when(val bean = mApi.getTime(id)){
                is ApiResult.Success -> onSuccess(bean.data.data.checkTime,bean.data.data.centrifugalTime)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    fun replace(oldUID:String,newUID:String,onSuccess:()->Unit,onError: (String) -> Unit){
        viewModelScope.launch {
            when(val bean = mApi.checkGroupReplace(oldUID,newUID)){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }
}